package com.atguigu.sparkcore.sharevariable

import java.util.concurrent.atomic.LongAccumulator

import com.atguigu.sparkcore.util.MySparkContextUtil

/**
 * description ：共享变量测试类,包括自定义累加器，广播变量测试
 * author      ：剧情再美终是戏 
 * mail        : 13286520398@163.com
 * date        ：Created in 2020/1/10 6:24
 * modified By ：
 * version:    : 1.0
 */
object TestShareVariable {

  def main(args: Array[String]): Unit = {

    // 创建sparkContext
    val sc = MySparkContextUtil.get(args)

    // 创建累加器并向sparkContext注册
    val accumulator = sc.longAccumulator("1")
    val acc = new Acc

    sc.register(acc)

    // 创建广播变量
    val listBroadcast = sc.broadcast(List(10, 20))

    // 创建rdd
    val rdd = sc.parallelize(1 to 20, 3)

    // 使用累加器
    rdd.foreach(x => {
      if (!listBroadcast.value.contains(x)) {
        acc.add(x)
      }
    })

    // 输出累加器内容
    println(acc.value)

    // 关闭sparkContext资源
    MySparkContextUtil.close(sc)
  }

}
